package com.zhuss.team.processor;

import com.zhuss.bean.bo.team.information.DeleteInfoBo;
import com.zhuss.bean.vo.team.information.DetailInfoVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.team.db.dao.ext.InformationDao;
import com.zhuss.team.db.entity.Information;
import com.zhuss.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: zhushanshuo
 * @Date: 2020/3/3 14:40
 */
@Component("informationDelete")
public class InformationDelete extends AbstractProcessor<Object, DeleteInfoBo> implements Processor<Object, DeleteInfoBo> {

    @Autowired
    private InformationDao informationDao;

    @Autowired
    @Qualifier("informationRead")
    private InformationRead infoRead;

    @Override
    public Object process(DeleteInfoBo bo) throws WpsException {
        Long infoId = bo.getInfoId();
        String title = ((DetailInfoVo) infoRead.detail(infoId)).getTitle();
        Information info = new Information();
        info.setInfoId(bo.getInfoId());
        info.setDeleted(bo.getDeleted());

        info.setModifierId(bo.getConcurrentUserId());
        info.setModifier(bo.getConcurrentUserName());
        info.setModifyTime(new Date());
        informationDao.delete(info);
        bo.setName(title);
        return null;
    }

    @Override
    public Object log(DeleteInfoBo param) throws WpsException {
        OpLog record = new OpLog();
        record.setMoudle(MoudleEnum.协同发布.key());
        record.setAction(LogActionEnum.信息发布删除.key());
        record.setTargetId(param.getInfoId());
        record.setTargetName(param.getName());
        record.setContent("参数: " + JsonUtil.toJson(param));
        record.setReason(param.getExceptionReason());

        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        log(record);
        return null;
    }

    @Override
    public void check(DeleteInfoBo deleteInfoBo) throws WpsException {

    }
}
